package com.fau.hm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.hm.action.PublicAction;
import com.fau.hm.entity.MedicalAdmin;
import com.fau.hm.entity.MedicalKeyValue;
import com.fau.hm.util.SystemConfig;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  订单字典表Action
 * @author yangzj
 * @datetime 2014年9月24日,下午3:28:58
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class DictionaryAction extends PublicAction implements ModelDriven<MedicalKeyValue> {

	private static final long serialVersionUID = -9111865783738307965L;
	
	private MedicalKeyValue keyVal = new MedicalKeyValue();
	
	/**
	 * @desc 跳转到字典表管理页面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:46:53
	 */
	@Action(value = "dic_init", results = {@Result(location = "dictionary_list.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 跳转至字典表管理添加界面 
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:47:53
	 */
	@Action(value = "dictionary_add", results = {@Result(location = "dictionary_add.jsp")})
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * @desc 跳转至字典表管理修改界面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:48:53
	 */
	@Action(value = "dictionary_edit", results = {@Result(location = "dictionary_edit.jsp")})
	public String edit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询字典信息
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:51:53
	 */
	@Action(value = "dictionary_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getDictionaryService().findDictionaryByPage(admin, keyVal, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 保存
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:52:53
	 */
	@Action(value = "dictionary_save")
	public String save(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Boolean flag =  this.baseAllService.getDictionaryService().saveDictionary(admin, keyVal);
		if(flag){
			this.map.put("success", true);
		}else{
			this.map.put("success", false);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 保存父级修改
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:53:53
	 */
	@Action(value = "dictionary_save_edit")
	public String saveEdit(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(keyVal.getId())){
			this.baseAllService.getDictionaryService().editSave(this.keyVal);
			this.map.put("success", true);
		}else{
			this.map.put("error", true);
			}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 删除字典表
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:54:53
	 */
	@Action(value = "dictionary_delete")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(keyVal.getId())){
			this.baseAllService.getDictionaryService().deleteDictionary(this.keyVal);
			this.map.put("success", true);
		}else{
			this.map.put("error", true);
		}
		formatJSON(response, this.map);
		return null;
	}

	/**
	 * @desc 验证字典表，表键
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:58:53
	 */
	@Action(value = "dictionary_ver_key")
	public String verKeyName(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(StringUtils.isNotBlank(keyVal.getKeyName())){
			Boolean flag = this.baseAllService.getDictionaryService().verKeyName(admin, keyVal.getKeyName());
			if(flag){
				this.map.put("result", true);
			}else{
				this.map.put("result", false);
			}
		}else{
			this.map.put("error", true);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询字典表类型集合
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月22日,上午11:41:08
	 */
	@Action(value = "dictionary_key_type_view")
	public String keyTypeView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getDictionaryService().findKeyTypeList();
		formatJSON(response, list);
		return null;
	}

	/**
	 * @desc 根据类型查询字典表数据
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月22日,下午8:45:41
	 */
	@Action(value = "dictionary_key_val_view")
	public String keyValView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(SystemConfig.DEPT_TYPE.equals(keyVal.getTypeName()) || SystemConfig.CENTER_TYPE.equals(keyVal.getTypeName())) {
			list = this.baseAllService.getDictionaryService().findKeyValList("0", SystemConfig.HM_SYSTEM_TYPE, keyVal.getTypeName());
		} else {
			list = this.baseAllService.getDictionaryService().findKeyValList(admin, SystemConfig.HM_SYSTEM_TYPE, keyVal.getTypeName());
		}
		formatJSON(response, list);
		return null;
	}
	
	@Override
	public MedicalKeyValue getModel() {
		return keyVal;
	}

}
